//
//  WBHomeViewController.swift
//  PersonalWeibo
//
//  Created by Liuwx on 2017/9/16.
//  Copyright © 2017年 Liuwx. All rights reserved.
//

import UIKit

//定义全局常量， 尽量使用private 防止到处访问

private let cellId = "cellId"


class WBHomeViewController: WBBaseViewController {

     lazy var statusList = [String]()
    
    
    /// 加载数据, 先调用自己本类的方法
    override func loadData() {
        
        //用网络工具加载网络数据
        
        
        WBNetworkManager.shared.statusList {list, isSueccess in
            print(list ?? 0)
            
        }
        
        
        
        print("开始加载数据 \(WBNetworkManager.shared)")
        
     DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) { 
        
        for i in 0..<15 {
            if self.isPullup {
                self.statusList.append("上拉 \(i)")
            }else {
            //            将数据插入到数组的顶部
            self.statusList.insert(i.description, at: 0)
            }
        }
        //结束刷新控件
        self.refreshControl?.endRefreshing()
        //恢复上拉刷新数据
        self.isPullup = false
        //刷新表格
        self.tableView?.reloadData()

        
        }
        
       
        
    }
    
    
    //显示好友
   @objc  func showFriends() {
        print(#function)

    let demoVC = WBDemoViewController()
    navigationController?.pushViewController(demoVC, animated: true)
    
    
    
    }
}


// MARK: - 表格数据源方法
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId)
        
        //设置cell
        cell?.textLabel?.text = statusList[indexPath.row]
        //返回cell
        return cell!
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    
}


    
extension WBHomeViewController {
    
    //重写父类的方法,extension里面的方法可以继承，系统本类的不可以
    override func setupTableView() {
        super.setupTableView()
        
        //设置导航栏按钮
        //无法高亮
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        //swift 调用OC 返回是instenceType 的时候，判断不出来是否是可选
//        let btn: UIButton = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        btn.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
        
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", tagret: self, action: #selector(showFriends))
        
        
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
    }
    
}

